<template>
  <div class="mb-20px">
    <el-button
      @click="
        () => {
          $router.back()
          delVisitedView(route)
        }
      "
      >返回</el-button
    ></div
  >
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="auto"
    >
      <el-form-item label="会员订单ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入会员订单ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="会员类型" prop="membershipName">
        <el-select
          v-model="queryParams.membershipName"
          placeholder="请选择"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="i in membershipSettingsList"
            :key="i.id"
            :label="i.membershipName"
            :value="i.membershipName as string"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付金额" prop="paymentAmount">
        <el-input
          v-model="queryParams.paymentAmount"
          placeholder="请输入支付金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
          type="number"
          min="0"
        />
      </el-form-item>
      <el-form-item label="有效期" prop="effectiveTime">
        <el-date-picker
          v-model="queryParams.effectiveTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="数量" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" type="primary"
          ><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button
        >
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="success" plain @click="handleExport" :loading="exportLoading">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
    >
      <!-- <el-table-column type="selection" width="55" /> -->
      <el-table-column label="会员订单ID" align="center" prop="id" />
      <el-table-column label="会员类型" align="center" prop="membershipName" />
      <el-table-column label="支付金额（元）" align="center" prop="paymentAmount" />
      <el-table-column label="数量" align="center" prop="number" />
      <el-table-column label="有效期" align="center" prop="effectiveTime" width="180px">
        <template #default="{ row }">
          {{ formatDate(row.effectiveTime[0]) }}-{{ formatDate(row.effectiveTime[1]) }}
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <!-- <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['biz:member-order:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { dateFormatter, formatDate } from '@/utils/formatTime'
import download from '@/utils/download'
import { MemberOrderApi, MemberOrder } from '@/api/biz/memberorder'
import { MembershipSettingsApi, MembershipSettings } from '@/api/biz/membershipsettings'
import { useTagsViewStore } from '@/store/modules/tagsView'

const { delVisitedView } = useTagsViewStore()
const route = useRoute()

/** 会员订单管理 列表 */
defineOptions({ name: 'MemberOrderUser' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const userId = +useRoute().params.id

const loading = ref(true) // 列表的加载中
const list = ref<MemberOrder[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined,
  membershipName: undefined,
  paymentAmount: undefined,
  number: undefined,
  effectiveTime: [],
  userId,
  nickname: undefined,
  mobile: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const membershipSettingsList = ref<MembershipSettings[]>([]) // 会员设置列表

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MemberOrderApi.getMemberOrderPage(queryParams)
    data.list.forEach((item) => {
      item.effectiveTime = item.effectiveTime.split(',')
    })
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const getMembershipSettingsList = async () => {
  const data = await MembershipSettingsApi.getMembershipSettingsPage({ pageNo: 1, pageSize: 100 })
  membershipSettingsList.value = data.list
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MemberOrderApi.deleteMemberOrder(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除会员订单管理 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await MemberOrderApi.deleteMemberOrderList(checkedIds.value)
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: MemberOrder[]) => {
  checkedIds.value = records.map((item) => item.id)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await MemberOrderApi.exportMemberOrder(queryParams)
    download.excel(data, '会员订单管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onActivated(() => {
  getList()
  getMembershipSettingsList()
})
</script>
